package cn.yunhe.action;

import cn.yunhe.entity.User;
import cn.yunhe.server.UserDaoServer;
import cn.yunhe.util.ActionUtil;

import java.util.Map;

public class UserAction extends ActionUtil {
    private User user;
    private String username;
    private String result;

    /**
     * 用户登录方法
     *
     * @return
     */
    public String login() {
        UserDaoServer userDaoServer = new UserDaoServer();
        Map map = getMapRequest();
        Map session = getMapSession(); //获取session对象
        if (user != null) {
            User user1 = userDaoServer.login(user.getUname(), user.getUpass()); //执行登录验证方法,验证成功则获取一个用户对象
            if (user1 != null) { // 若数据库存在该用户则跳转成功界面并将用户信息存入session,否则跳转失败界面
                session.put("user", user1);
                return SUCCESS;
            } else {
                Integer count = (Integer) session.get("count"); //获取session内部计数器,若该值为空说明是第一次记录,把该值赋予初始值1
                if (count == null){
                    count = 1;
                    session.put("count", count);
                }else{
                    count++;
                    session.put("count", count);
                }
                map.put("count", count); //将count值存入request对象中
                map.put("result", "密码错误!! 请输入正确的用户名");
                return LOGIN;
            }
        } else { //user为空则获取用户信息失败!!
            map.put("result", "登录出错<a href='javascript:history.go(-1)'>返回</a>");
            return ERROR;
        }
    }

    /**
     * 用户注册方法
     *
     * @return
     */
    public String register() {
        UserDaoServer userDaoServer = new UserDaoServer();
        Map map = getMapRequest();
        boolean flag = userDaoServer.register(user); //执行注册方法
        if (flag) {
            map.put("result", "注册成功!!<a href='/login.jsp'>去登陆</a>");
            return SUCCESS;
        } else {
            map.put("result", "注册失败!!<a href='javascript:history.go(-1)'>返回</a>");
            return ERROR;
        }
    }

    /**
     * 通过用户查询用户是否存在
     *
     * @return
     */
    public String getUserByName() {
        UserDaoServer userDaoServer = new UserDaoServer();
        if (username != null) {
            User user1 = userDaoServer.getUserByName(username); //进行用户名验证
            if (user1 == null){ //返回对象为空说明不存在该用户名,返回false,反之返回true
                setResult("false");
                return SUCCESS;
            }else {
                setResult("true");
                return SUCCESS;
            }
        } else {
            return ERROR;
        }
    }

    /**
     * 用户注销方法
     * @return
     */
    public String userLogout(){
        Map session = getMapSession();
        session.clear(); //清除session里面的内容,返回登录界面
        return SUCCESS;
    }


    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}
